package org.sy.lab.多线程基础._4_执行器._1_创建线程池._3_newSingleThreadExecutor;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.sy.lab.多线程基础._4_执行器._1_创建线程池._1_newFixedThreadPool.MyThread;

//创建一个单线程化的线程池，它只会用唯一的工作线程来执行任务，保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行
public class Main {
	
	public static void main(String[] args) {
		
		ExecutorService executor = Executors.newSingleThreadExecutor();
		
		for (int i = 0; i < 5; i++) {
			MyThread myThread = new MyThread("任务" + (i+1));
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			executor.execute(myThread);
		}
		
		for (int i = 5; i < 10; i++) {
			MyThread myThread = new MyThread("任务" + (i+1));
			try {
				Thread.sleep(300);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			executor.execute(myThread);
		}
		
		for (int i = 10; i < 15; i++) {
			MyThread myThread = new MyThread("任务" + (i+1));
			try {
				Thread.sleep(10);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			executor.execute(myThread);
		}
		executor.shutdown();
	}
}
